﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace CodePlex.Resourceful.Amazon.S3
{

    internal class InternalCopyObjectResult
    {


        // good
        //<CopyObjectResult>
        //   <LastModified>timestamp</LastModified>
        //   <ETag>"etag"</ETag>
        //</CopyObjectResult>

        // bad
        //<Error>
        //  <Code>error_code</Code>
        //  <Message>error_message</Message>
        //  <RequestId>request_id</RequestId>
        //  <HostId>host_id</HostId>
        //</Error>

        public readonly bool IsError;
        public readonly string LastModified;
        public readonly string ETag;
        public readonly string ErrorCode;
        public readonly string ErrorMessage;
        public readonly string RequestID;
        public readonly string HostID;




        internal InternalCopyObjectResult(bool isError, 
            string lastModified, string etag,
            string errorCode, string errorMessage, string requestID, string hostID)
        {
            this.IsError = isError;
            this.LastModified = lastModified;
            this.ETag = etag;
            this.ErrorCode = errorCode;
            this.ErrorMessage = errorMessage;
            this.RequestID = requestID;
            this.HostID = hostID;
        }


        public static InternalCopyObjectResult Parse(XmlReader reader)
        {

            bool isError = true;
            string lastModified = null;
            string etag = null;
            string errorCode = null;
            string errorMessage = null;
            string requestID = null;
            string hostID = null;

            while (reader.Read())
            {
                if (reader.NodeType == XmlNodeType.Element)
                {
                    if (reader.Name == "CopyObjectResult")
                    {
                        isError = false;
                    }
                    else if (reader.Name == "Error")
                    {
                        isError = true;
                    }
                    else if (reader.Name == "LastModified")
                    {
                        lastModified = Utils.ReadString(reader);
                    }
                    else if (reader.Name == "ETag")
                    {
                        etag = Utils.ReadString(reader);
                    }
                    else if (reader.Name == "Code")
                    {
                        errorCode = Utils.ReadString(reader);
                    }
                    else if (reader.Name == "Message")
                    {
                        errorMessage = Utils.ReadString(reader);
                    }
                    else if (reader.Name == "RequestId")
                    {
                        requestID = Utils.ReadString(reader);
                    }
                    else if (reader.Name == "HostId")
                    {
                        hostID = Utils.ReadString(reader);
                    }
                   
                   
                }

                if (reader.NodeType == XmlNodeType.EndElement && Utils.In(reader.Name ,"CopyObjectResult","Error"))
                {
                    return new InternalCopyObjectResult(isError,lastModified,etag,errorCode,errorMessage,requestID,hostID);
                }

            }

            throw CodePlexException.Format("Failed to parse copy-object-result.");
        }


    }
}
